package com.example.manage_c20086.api;
import com.pojo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("api/user")
public class UsersApi {
    @Autowired
    private RestTemplate restTemplate;

    private static final String REST_URL_PREFIX="http://USERS-MANAGEMENT/user";
    private static final String IDENTIFYING="identifying";
    @GetMapping("/getCheckcode")
    public Result<?> getCheckcode(@RequestParam String uEmail){
        return restTemplate.getForObject(REST_URL_PREFIX+"/getCheckcode/"+uEmail,Result.class);
    }
    @GetMapping ("/regiest")
    public Result<String>regiest(@RequestParam String uEmail,
                                 @RequestParam String uPassword,
                                 @RequestParam String CheckCode){
        return restTemplate.getForObject(REST_URL_PREFIX+"/regiest/"+uEmail+"/"+uPassword+"/"+CheckCode
                ,Result.class);
    }
    @PostMapping("/login")
    public Result<?>login(@RequestParam String uEmail, @RequestParam String uPassword){
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        HttpHeaders headers = new HttpHeaders();
        map.add("uEmail",uEmail);
        map.add("uPassword",uPassword);
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return restTemplate.postForObject(REST_URL_PREFIX+"/login",request,Result.class);
    }
    @PostMapping("/updateps")
    public Result<?>updateps(@RequestParam String oldPassword, @RequestParam String newPassword, HttpServletRequest httpServletRequest){
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("oldPassword",oldPassword);
        map.add("newPassword",newPassword);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return  restTemplate.postForObject(REST_URL_PREFIX+"/updateps",request,Result.class);
    }
    @GetMapping("/updatetp")
    public Result<?>updatetp( Integer  uID, Integer uType,HttpServletRequest httpServletRequest){
        MultiValueMap<String, Object> map= new LinkedMultiValueMap<>();
        map.add("uID",uID);
        map.add("uType",uType);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.add(IDENTIFYING,httpServletRequest.getHeader(IDENTIFYING));
        HttpEntity<MultiValueMap<String,Object>> request = new HttpEntity<MultiValueMap<String,Object>>(map, headers);
        return  restTemplate.postForObject(REST_URL_PREFIX+"/updatetp",request,Result.class);
    }
}
